﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Asplode.Events;
using Microsoft.Xna.Framework;
using BEPUphysics.Entities;
using Asplode.GameEntity.Characters;

namespace Asplode.GameWorld
{
    public class GameWorld
    {
        private List<Human> _humans = new List<Human>();
        public List<Human> Humans
        {
            get
            {
                return _humans;
            }
        }
        private List<Zombie> _zombies = new List<Zombie>();
        public List<Zombie> Zombies
        {
            get
            {
                return _zombies;
            }
        }
        private List<Entity> _entList = new List<Entity>();
        public List<Entity> EntList
        {
            get
            {
                return _entList;
            }
        }
        private Entity Player;

        private EventDispatcher EventDispatcher;


        public GameWorld(EventDispatcher handler)
        {
            EventDispatcher = handler;
        }


        public void Update(GameTime GameTime)
        {

        }

        public void ClearWorld()
        {
            while (_entList.Count > 0)
            {
                EventDispatcher.Handle<RemoveEntityEvent>(
                    new RemoveEntityEvent(_entList[_entList.Count - 1]));
            }
            Player = null;
        }

        public void Initialize()
        {
            Entity Floor = EntityFactory.createFloor();
            EventDispatcher.Handle<BoxSpawnEvent>(new BoxSpawnEvent(Floor as Box));

            Entity Zombie = EntityFactory.createZombie(new Vector3(5.0f, 1.5f, 5.0f));
            EventDispatcher.Handle<ZombieSpawnEvent>(new ZombieSpawnEvent(Zombie));

            Player = EntityFactory.createPlayer(new Vector3(0, 1.5f, 0));
            EventDispatcher.Handle<HumanSpawnEvent>(new HumanSpawnEvent(Player));
        }

        public class EntityFactory
        {
            public static Entity createZombie(Vector3 pos)
            {
                return new Cylinder(pos, 2, 1, 5);
            }

            public static Entity createFloor()
            {
                return new Box(Vector3.Zero, 800, 1, 600);
            }

            public static Entity createPlayer(Vector3 pos)
            {
                return new Cylinder(pos, 2, 1, 1);
            }
        }
    }
}
